package com.kkb.springcloudbill.utils;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class DateConverter implements Converter<String, Date> {
        private static final List<String> formarts = new ArrayList<>(4);
        static{
            formarts.add("yyyy-MM");
            formarts.add("yyyy-MM-dd");
            formarts.add("yyyy-MM-dd hh:mm");
            formarts.add("yyyy-MM-dd hh:mm:ss");
        }

        @Override
        public Date convert(String source) {
            String value = source.trim();
//            System.out.println(value);
            if ("".equals(value)) {
                return null;
            }
            if (source.matches("^\\d{4}-\\d{1,2}$")) {
                return parseDate(source, formarts.get(0));
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
                return parseDate(source, formarts.get(1));
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(2));
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                return parseDate(source, formarts.get(3));
            } else {
                throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
            }
        }
    /**
     * 格式化日期
     * @param dateStr String 字符型日期 * @param format String 格式
     * @return Date 日期
     */
    public  Date parseDate(String dateStr, String format) {
        Date date=null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(format);
            System.out.println(dateStr + " " + format);
//            String format1 = dateFormat.format(dateFormat.parse(dateStr));
            date = dateFormat.parse(dateStr);
            System.out.println(date);
        } catch (Exception e) {
        }
        return date;
    }
}